﻿using System;
using System.Collections.Generic;

using SBPweb.Modules.Modularity;

using SBPweb.MediaCenter.Common.Collections;

namespace SBPweb.MediaCenter.Modules.Downloader.Torrent
{

    public class TorrentServiceBase : DownloaderServiceBase, ITorrentService
    {

        #region Private / protected fields

        private TorrentManager manager = new TorrentManager();

        #endregion

        #region Constructor logic

        public TorrentServiceBase(IModule parentModule)
            : base(parentModule)
        {
        }

        #endregion

        #region Property accessors

        #endregion

        #region Methods

        public void InitializeSettings(string savePath, int listenPort, int uploadSlots, int maxConnections, int maxDownloadSpeed, int maxUploadSpeed, bool fastResumeEnabled)
        {
            manager.InitializeEngine(savePath, listenPort, uploadSlots, maxConnections, maxDownloadSpeed, maxUploadSpeed, fastResumeEnabled);
        }

        public override IDownloaderFile OpenAndGet(Uri uri)
        {
            TorrentFile file = new TorrentFile(uri);
            base.downloadList.Add(file.PriorityAsInt, file);
            manager.AddTorrent(file);
            return file;
        }

        public override void Stop()
        {
            base.Stop();
            this.manager.FinalizeEngine();
        }

        #region Service compatibility for polimorphism

        public override void Start(IDownloaderFile file)
        {
            this.Start(ConvertDownloaderFileToTorrentFile(file));
        }

        public override void Stop(IDownloaderFile file)
        {
            this.Stop(ConvertDownloaderFileToTorrentFile(file));
        }

        public override void Pause(IDownloaderFile file)
        {
            this.Pause(ConvertDownloaderFileToTorrentFile(file));
        }

        #endregion

        #region Effective torrent list management

        public void Start(IList<ITorrentFile> files)
        {
            base.Start(ListHelper.ConvertTo<ITorrentFile, IDownloaderFile>(files, ConvertTorrentFileToDownloaderFile));
        }

        public void Stop(IList<ITorrentFile> files)
        {
            base.Stop(ListHelper.ConvertTo<ITorrentFile, IDownloaderFile>(files, ConvertTorrentFileToDownloaderFile));
        }

        public void Pause(IList<ITorrentFile> files)
        {
            base.Pause(ListHelper.ConvertTo<ITorrentFile, IDownloaderFile>(files, ConvertTorrentFileToDownloaderFile));
        }

        #endregion

        #region Effective torrent item management

        public void Start(ITorrentFile file)
        {
            manager.StartTorrent(file);
        }

        public void Stop(ITorrentFile file)
        {
            manager.StopTorrent(file);
        }

        public void Pause(ITorrentFile file)
        {
            manager.PauseTorrent(file);
        }

        #endregion

        #region Helpers / converters

        internal static IDownloaderFile ConvertTorrentFileToDownloaderFile(ITorrentFile file)
        {
            IDownloaderFile idf = file as IDownloaderFile;
            if (idf == null)
            {
                throw new NotSupportedException(string.Format("Can not convert file {0} to IDownloaderFile!", file));
            }
            return idf;
        }

        internal static ITorrentFile ConvertDownloaderFileToTorrentFile(IDownloaderFile file)
        {
            ITorrentFile itf = file as ITorrentFile;
            if (itf == null)
            {
                throw new NotSupportedException(string.Format("Can not convert file {0} to ITorrentFile!", file));
            }
            return itf;
        }

        #endregion

        #endregion

    }

}